﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using TotalCrytography.Protector;
using System.IO;
using TotalCrytography.SystemFile;

namespace TotalCrytography
{
    public partial class DecryptGUI : Form
    {
        String _privateKey;
        Symmetric_Provider _sp;
        private String _strOutputFile;
        private List<String> _lstSelectFile;
        private String _pass;

        public DecryptGUI()
        {
            InitializeComponent();
        }

        public DecryptGUI(List<String> selectFile,String curPass, String privateKey)
        {
            InitializeComponent();
            _lstSelectFile = selectFile;
            _pass = curPass;
            _privateKey = privateKey;
        }

        
        private void DecryptGUI_Load(object sender, EventArgs e)
        {
            string temp = _lstSelectFile[0].Substring(0, _lstSelectFile[0].LastIndexOf('\\') + 1);
            tbPath.Text = temp;
        }



        private void btBrowse_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fd = new FolderBrowserDialog();
            if (fd.ShowDialog() == DialogResult.OK)
                tbPath.Text = fd.SelectedPath + "\\";
        }

        private void btExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btDecrypt_Click(object sender, EventArgs e)
        {
            try
            {
                Symmetric_Provider sp = new Symmetric_Provider();
                for (int i = 0; i < _lstSelectFile.Count; i++)
                {
                    String inputFile = _lstSelectFile[i];
                    String outputFile = _lstSelectFile[i];
                    int index = outputFile.LastIndexOf('.');
                    //check ext file is ".enc"
                    if (index > 0 && outputFile.Substring(index) == ".enc")
                        outputFile = outputFile.Remove(index);

                    MyFile f = new MyFile(inputFile);
                    f.Decrypt(inputFile, tbPath.Text, _pass, _privateKey);
                    
                }
                MessageBox.Show("Decrypt Sucessfully !");
                DialogResult = DialogResult.OK;
                this.Close();
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message, "Error", MessageBoxButtons.OK);
            }
        }

       
    }
}
